package com.example.concurrentprogramming8016.future.completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-08 22:13
 * 用两个线程T1和T2来完成烧水泡茶程序，
 * T1负责洗水壶、烧开水、泡茶这三道工序，T2负责洗茶壶、洗茶杯、拿茶叶三道工序，其中
 * T1在执行泡茶这道工序时需要等待T2完成拿茶叶的工序。
 * 基于
 */
public class CompletableFutureDemo3 {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("洗茶壶");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("洗茶杯");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("拿茶叶");
                return "铁观音";
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        //看铺累的宝
        future.thenAccept((str)->{
            try {
                System.out.println("洗水壶");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("烧开水");
                TimeUnit.SECONDS.sleep(1);
                System.out.println("泡茶"+str);
                System.out.println("上茶" + str);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).join();
    }
}
